package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.HomeSurround;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.reflect.TypeToken;

public class DoctorSheQuyiyuanActivity extends BaseActivity {

	private TextView tv_num;
	private TextView tv_shenqingaaaa;
	private String name;
	private Map<String, String> planList = new HashMap<String, String>();
	private HomeSurround surround;
	private String poId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shequ_yiyuan);
		initView();
	}

	private void initView() {
		tv_num = (TextView) findViewById(R.id.tv_num);
		tv_quyu = (TextView) findViewById(R.id.tv_quyu);
		tv_shenqingaaaa = (TextView) findViewById(R.id.tv_shenqingaaaa);
		tv_shenqingaaaa.setOnClickListener(this);
		getDate();
		getShequ();
		initTitleBar();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.tv_shenqingaaaa:
			tv_shenqingaaaa.setOnClickListener(DoctorSheQuyiyuanActivity.this);
			tv_shenqingaaaa.setClickable(false);
			getShenqing();
			break;

		default:
			break;
		}
	}

	private void getDate() {
		name = getIntent().getStringExtra("Name");
		poId = getIntent().getStringExtra("poId");
		surround = (HomeSurround) getIntent().getSerializableExtra(
				"homesurround");
		tv_quyu.setText(name);
	};

	private Handler Handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String dataStr = data.getString(HttpCons.DATA);
			if ("".equals(dataStr)) {
				tv_num.setText("0");
			} else {
				if (state.equals("001")) {
					planList = HttpServer.gson.fromJson(dataStr,
							new TypeToken<Map<String, String>>() {
							}.getType());
					tv_num.setText(planList.get("applyCount"));
					Toast.makeText(DoctorSheQuyiyuanActivity.this, msg, 0)
							.show();
				} else {
					Toast.makeText(DoctorSheQuyiyuanActivity.this, msg, 0)
							.show();
				}
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(DoctorSheQuyiyuanActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void getShequ() {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("poiId", poId);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.querHospitalApplicationInformation,
				parameter, Handler);
	}

	private Handler iiHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String dataStr = data.getString(HttpCons.DATA);
			dataStr = dataStr;
			if (state.equals("001")) {
				tv_shenqingaaaa.setText("已申请");
				planList.clear();
				getShequ();

				Toast.makeText(DoctorSheQuyiyuanActivity.this, msg, 0).show();

			} else {
				Toast.makeText(DoctorSheQuyiyuanActivity.this, msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(DoctorSheQuyiyuanActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(R.string.co);

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private TextView tv_quyu;

	private void getShenqing() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("hospitalName", name);
		map.put("useraccount", MyConst.getACCOUNT());
		map.put("latitude", surround.getLat());
		map.put("longitude", surround.getIng());
		map.put("provice_name", surround.getProvince_name());
		map.put("city_name", surround.getCity_name());
		// map.put("county_name", surround.getCounty_name());
		map.put("provice_code", surround.getProvince_code());
		map.put("city_code", surround.getCity_code());
		// map.put("county_code", surround.getCounty_code());
		map.put("poiId", surround.getPoiId());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.addInfirmaryInfo, parameter, iiHandler);
	}

}
